package org.jeecg.modules.pm.business.captcha;

import lombok.RequiredArgsConstructor;
import org.jeecg.modules.project.common.util.RedisUtils;
import org.jeecg.modules.pm.model.captcha.vo.CaptchaVO;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * description: 验证码Service
 * taskId：
 * <pre>
 * change history:
 * date             defect#             person                comments
 * ---------------------------------------------------------------------
 * 2023/10/16       ********         zhaochaoyue        create file
 * </pre>
 *
 * @author: zhaochaoyue
 * @date: 2023/10/16 11:08
 * @copyright: 2023
 */
@Service
@RequiredArgsConstructor
public class CaptchaService {

//    @Value("${server.session.timeout:300}")
    private Long timeout =10L;

    private final RedisUtils redisUtils;


    private final String CAPTCHA_KEY = "captcha:verification:";

    public CaptchaVO cacheCaptcha(String captcha){
        //生成一个随机标识符
        String captchaKey = UUID.randomUUID().toString();

        //缓存验证码并设置过期时间
        redisUtils.set(CAPTCHA_KEY.concat(captchaKey),captcha,timeout);

        CaptchaVO captchaVO = new CaptchaVO();
        captchaVO.setCaptchaKey(captchaKey);
        captchaVO.setExpire(timeout);

        return captchaVO;
    }

}
